#!/bin/bash
###
 # @Author: lineCode
 # @Date: 2024-12-17 10:08:22
 # @LastEditors: lineCode
 # @LastEditTime: 2025-09-03 00:10:00
 # @FilePath: /backend/docker/copy.sh
###

# 定义颜色输出函数
color_echo() {
    local color=$1
    local msg=$2
    case $color in
        red)    echo -e "\033[31m$msg\033[0m" ;;
        green)  echo -e "\033[32m$msg\033[0m" ;;
        yellow) echo -e "\033[33m$msg\033[0m" ;;
        *)      echo "$msg" ;;
    esac
}

# 定义要复制的文件列表：源路径 目标路径
files=(
    "../deploy/shorthand-gateway.jar" "./shorthand/gateway/jar/shorthand-gateway.jar"
    "../deploy/shorthand-auth.jar" "./shorthand/auth/jar/shorthand-auth.jar"
    "../deploy/shorthand-modules-system.jar" "./shorthand/modules/system/jar/shorthand-modules-system.jar"
    "../deploy/shorthand-modules-front.jar" "./shorthand/modules/front/jar/shorthand-modules-front.jar"
    "../deploy/shorthand-modules-content.jar" "./shorthand/modules/content/jar/shorthand-modules-content.jar"
)

# 检查文件列表是否成对
if (( ${#files[@]} % 2 != 0 )); then
    color_echo red "错误：文件列表格式不正确，需要成对的源路径和目标路径"
    exit 1
fi

# 遍历文件列表进行复制
for ((i=0; i<${#files[@]}; i+=2)); do
    src="${files[i]}"
    dest="${files[i+1]}"
    service_name=$(basename "$src" .jar)  # 从文件名提取服务名

    color_echo yellow "开始复制 $service_name..."

    # 检查源文件是否存在
    if [ ! -f "$src" ]; then
        color_echo red "错误：源文件 $src 不存在，跳过复制"
        continue
    fi

    # 创建目标目录（如果不存在）
    dest_dir=$(dirname "$dest")
    if [ ! -d "$dest_dir" ]; then
        mkdir -p "$dest_dir" || {
            color_echo red "错误：无法创建目标目录 $dest_dir，跳过复制"
            continue
        }
    fi

    # 删除旧文件并复制新文件
    rm -f "$dest"  # 使用-f避免文件不存在时的错误提示
    if cp "$src" "$dest"; then
        color_echo green "成功复制 $service_name 到 $dest"
    else
        color_echo red "错误：复制 $service_name 失败"
    fi
done

color_echo green "所有复制操作已完成"